package com.geeklan.smart.ui.dialog;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.geeklan.smart.R;
import com.geeklan.smart.databinding.DialogContentSmartFoodBinding;

public class SmartFoodBottomSheetDialog extends BaseAlertDialog<DialogContentSmartFoodBinding>{
    private int smartFoodBowl = 0;
    private int foodGrams = 10;

    public int getSmartFoodBowl() {
        return smartFoodBowl;
    }

    public int getFoodGrams() {
        return foodGrams;
    }

    public void setFoodGrams(int foodGrams) {
        this.foodGrams = foodGrams;
        switch (foodGrams){
            case 10:
                mBinding.rb10.setChecked(true);
                break;
            case 20:
                mBinding.rb20.setChecked(true);
                break;
            case 30:
                mBinding.rb30.setChecked(true);
                break;
            case 40:
                mBinding.rb40.setChecked(true);
                break;
            case 50:
                mBinding.rb50.setChecked(true);
                break;
            case 60:
                mBinding.rb60.setChecked(true);
                break;
        }
        setContentText();
    }

    private void setContentText(){
        String context = getContext().getString(R.string.settings_smart_food_content);
        String context1 = String.format(context,foodGrams);
        SpannableStringBuilder builder = new SpannableStringBuilder(context1);
        ForegroundColorSpan redSpan = new ForegroundColorSpan(getContext().getColor(R.color.color_fe8357));
        builder.setSpan(redSpan, 8, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mBinding.tvContext.setText(builder);
    }

    public SmartFoodBottomSheetDialog(@NonNull Context context, String title, int smartFoodBowl) {
        super(context);
        this.smartFoodBowl = smartFoodBowl;
        mBinding.scStatus.setChecked(smartFoodBowl == 1);
        setTitle(title);
        setPositiveButtonText(getContext().getString(R.string.save));
        setTopCloseButtonVisibility(true);
        mBinding.scStatus.setOnClickListener(v -> {
            if(mBinding.scStatus.isChecked()){
                this.smartFoodBowl = 1;
            }else {
                this.smartFoodBowl = 0;
            }
        });
        mBinding.rb10.setOnClickListener(v -> {
            foodGrams = 10;
            setContentText();
        });
        mBinding.rb20.setOnClickListener(v -> {
            foodGrams = 20;
            setContentText();
        });
        mBinding.rb30.setOnClickListener(v -> {
            foodGrams = 30;
            setContentText();
        });
        mBinding.rb40.setOnClickListener(v -> {
            foodGrams = 40;
            setContentText();
        });
        mBinding.rb50.setOnClickListener(v -> {
            foodGrams = 50;
            setContentText();
        });
        mBinding.rb60.setOnClickListener(v -> {
            foodGrams = 60;
            setContentText();
        });
    }



    @Override
    protected DialogContentSmartFoodBinding getContentViewBinding(LayoutInflater inflater) {
        return DialogContentSmartFoodBinding.inflate(inflater);
    }
}
